package com.nontax.test.common;

import com.nontax.test.common.domain.BaseRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class BaseController {


    protected String generateRequest(BaseRequest request,String url){
        request.setIp(request.getIp().replaceAll("http://",""));
        request.setAddress(StringUtils.isNotEmpty(request.getAddress()) ? request.getAddress() : url);
        request.setAppId(StringUtils.isNotEmpty(request.getAppId()) ? request.getAppId() : "bs");
        request.setAppKey(StringUtils.isNotEmpty(request.getAppKey()) ? request.getAppKey() : "999");
        return "http://"+request.getIp()+":"+request.getPort()+"/"+request.getAddress();
    }

    protected String getSid(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        return session.getAttribute("sid").toString();
    }
}
